<template>
	<xn-form-container
		:title="formData.id ? '编辑期货统计' : '增加期货统计'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="统计时间节点：" name="timePos">
						<a-input v-model:value="formData.timePos" placeholder="请输入统计时间节点" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="合约代码：" name="contractCode">
						<a-input v-model:value="formData.contractCode" placeholder="请输入合约代码" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="合约名称：" name="contractName">
						<a-input v-model:value="formData.contractName" placeholder="请输入合约名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="来源：" name="datasource">
						<a-input v-model:value="formData.datasource" placeholder="请输入来源" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="历史批次号：" name="historyBatchId">
						<a-input v-model:value="formData.historyBatchId" placeholder="请输入历史批次号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="策略id：" name="strategyId">
						<a-input v-model:value="formData.strategyId" placeholder="请输入策略id" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="买涨止损次数：" name="upLossTimes">
						<a-input v-model:value="formData.upLossTimes" placeholder="请输入买涨止损次数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="买跌止损次数：" name="downLossTimes">
						<a-input v-model:value="formData.downLossTimes" placeholder="请输入买跌止损次数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="策略范围交易次数：" name="times">
						<a-input v-model:value="formData.times" placeholder="请输入策略范围交易次数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="总止损次数：" name="lossTimes">
						<a-input v-model:value="formData.lossTimes" placeholder="请输入总止损次数" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="总收益：" name="profits">
						<a-input v-model:value="formData.profits" placeholder="请输入总收益" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="止损频率：" name="lossRate">
						<a-input v-model:value="formData.lossRate" placeholder="请输入止损频率" allow-clear />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="qhStatisticsForm">
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import qhStatisticsApi from '@/api/biz/qhStatisticsApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				qhStatisticsApi
					.qhStatisticsSubmitForm(formDataParam, formDataParam.id)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
					submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
